//
//  LOLFavorViewController.m
//  LOLXXbox
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLFavorViewController.h"
#import "LOLFavorTableViewCell.h"
#import "LSZDataBaseSinglenton.h"
#import <UIImageView+WebCache.h>
#import "LSZDataBaseSinglenton.h"
@interface LOLFavorViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation LOLFavorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的收藏";
    
    // 重写系统自带按钮事件
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // 数据库
    LSZDataBaseSinglenton *dataBaseHandle = [LSZDataBaseSinglenton shareDataBase];
    // 创建收藏表
    [dataBaseHandle createTable];
    // 用数组接受一下
    self.datasource = [[NSArray alloc] initWithArray:[dataBaseHandle selectDataBasearray]];
    
    [self.view addSubview:self.tableView];
    
    [_tableView reloadData];
    
}

//重写系统自带编辑按钮的点击事件
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    //    通过tag获取tableView
    UITableView *tableView = (UITableView *)[self.view viewWithTag:1001];
    //    让tableView处于可编辑状态
    [tableView setEditing:editing animated:animated];
}

//编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
//允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
// 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 先删除数据
        [[LSZDataBaseSinglenton shareDataBase]deleteSQL:self.datasource[0][indexPath.row]];
        // 再删除cell
        [self.datasource[1] removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
// 控制移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//移动完成之后
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSArray *array = self.datasource[sourceIndexPath.row];
    [self.datasource[1] removeObjectAtIndex:sourceIndexPath.row];
    [self.datasource[1] insertObject:array atIndex:destinationIndexPath.row];
    [tableView reloadData];
}


- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[LOLFavorTableViewCell class] forCellReuseIdentifier:@"cell"];
        
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return [self.datasource[1] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LOLFavorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *array = [[NSArray alloc] initWithObjects:self.datasource[2][indexPath.row],self.datasource[1][indexPath.row],nil];
    cell.DataSource = array;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
